package com.good.child.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.good.child.R;

import androidx.annotation.Nullable;


public class ActionBarView extends LinearLayout {
    private Context mContext;
    private TextView tvLeft;
    private TextView tvTitle;
    private TextView tvRight;

    public ActionBarView(Context context) {
        this(context, null);
    }

    public ActionBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ActionBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        // 加载布局
        LayoutInflater.from(context).inflate(R.layout.view_base_actionbar, this);
        TypedArray styleAttrs = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ActionBarView, defStyleAttr, 0);
        String backName = styleAttrs.getString(R.styleable.ActionBarView_backName);
        String nextName = styleAttrs.getString(R.styleable.ActionBarView_nextName);
        String titleName = styleAttrs.getString(R.styleable.ActionBarView_titleName);
        tvLeft = findViewById(R.id.tv_left);
        if (backName.isEmpty() || backName.equals("")) {
            tvLeft.setText("返回");
        } else {
            tvLeft.setText(backName);
        }
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText(titleName);
        tvRight = findViewById(R.id.tv_right);
        if (nextName.isEmpty() || nextName.equals("")) {
            tvRight.setText("");
        } else {
            tvRight.setText(nextName);
        }
    }

    public ActionBarView setTvRightText(String nextName) {
        this.tvRight.setText(nextName);
        return this;
    }

    public ActionBarView setTvRightOnclick(OnClickListener l) {
        this.tvRight.setOnClickListener(l);
        return this;
    }

    public ActionBarView setTvLeftText(String backName) {
        this.tvLeft.setText(backName);
        return this;
    }

    public ActionBarView setTLeftOnclick(OnClickListener l) {
        this.tvLeft.setOnClickListener(l);
        return this;
    }

    public ActionBarView setTvTitleText(String titleName) {
        this.tvTitle.setText(titleName);
        return this;
    }

    public ActionBarView setTvTitleOnclick(OnClickListener l) {
        this.tvTitle.setOnClickListener(l);
        return this;
    }


}
